package com.example.ljw.paperlive.ui.famliy;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.example.ljw.paperlive.R;
import com.example.ljw.paperlive.adapter.FamliyRecyclerDaylistAdapter;
import com.example.ljw.paperlive.base.BaseFragment;
import com.example.ljw.paperlive.bean.FamliyBean;
import com.example.ljw.paperlive.config.URLConfig;
import com.example.ljw.paperlive.widget.DividerItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by LJW on 2016/11/24.
 */

public class FamliyInnerFragment extends BaseFragment implements FamliyContract.IFamliyView{
    public static final String TAG="+++++++++++++++++";
    //recyclerView
    private RecyclerView recycler_famliy_daylist;
    //recyclerview适配器
    private FamliyRecyclerDaylistAdapter mDaylistAdapter;
    //日榜的list
    private List<FamliyBean.ListBean> mListDaylist=new ArrayList<>();
    //map
    private Map<String,String>  mMapDayList=new HashMap<>();
    private Map<String,String>  mMapHourList=new HashMap<>();
    private Map<String,String>  mMapRiseList=new HashMap<>();
    private Map<String,String>  mMapNewsList=new HashMap<>();


    //mvp
    private  FamliyContract.IFamliyPresenter iFamliyPresenter=null;
    //progressbar
    private ProgressBar progressBarFragment;

    //tab的下标
    private int mTabIndex;

    public static FamliyInnerFragment newInstance(int tabindex) {
        FamliyInnerFragment fragment = new FamliyInnerFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("mTabindex", tabindex);

        fragment.setArguments(bundle);
        return fragment;
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getArguments()!=null){
            mTabIndex=getArguments().getInt("mTabindex");

        }


    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        switch (mTabIndex){
            //家族日榜
            case 1:
                return inflater.inflate(R.layout.fragment_famliy_daylist,container,false);
            //小时榜
            case 2:
                return inflater.inflate(R.layout.fragment_famliy_daylist,container,false);
            //飙升榜
            case 3:
                return inflater.inflate(R.layout.fragment_famliy_daylist,container,false);
            //最新家族
            case 4:
                return inflater.inflate(R.layout.fragment_famliy_daylist,container,false);
        }
        return null;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recycler_famliy_daylist= (RecyclerView) view.findViewById(R.id.recycler_famliy_daylist);
        progressBarFragment= (ProgressBar) view.findViewById(R.id.progressBar_fragment);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        startMvp();

    }
    //初始化日榜map集合
    private void initDayListMap() {

        mMapDayList.put(URLConfig.URLFamliy.TAG_ID,URLConfig.URLVALUE.TAG_IDDAYLIST);
        mMapDayList.put(URLConfig.URLFamliy.PAGEINDEX,URLConfig.URLVALUE.PAGEINDEX);
        mMapDayList.put(URLConfig.URLFamliy.SIGNATURE,URLConfig.URLVALUE.SIGNATURE_DAYLIST);
        mMapDayList.put(URLConfig.URLFamliy.PUBLICKEY,URLConfig.URLVALUE.PUBLICKEY);
        mMapDayList.put(URLConfig.URLFamliy.TIMESTAMP,URLConfig.URLVALUE.TIMESTAMP_DAYLIST);
        mMapDayList.put(URLConfig.URLFamliy.TOKEN,URLConfig.URLVALUE.TOKEN);
        mMapDayList.put(URLConfig.URLFamliy.NONCE,URLConfig.URLVALUE.NONCEDAYLIST);

    }


    private void startMvp() {
        iFamliyPresenter=new FamliyPresenter(this);
        switch (mTabIndex){
            //家族日榜
            case 1:
                initDayListMap();
                iFamliyPresenter.getFamliyData(mMapDayList);
            //小时榜
            case 2:
                initHourList();
                iFamliyPresenter.getFamliyData(mMapHourList);
            //飙升榜
            case 3:
                initRiseListMap();
                iFamliyPresenter.getFamliyData(mMapRiseList);
            //最新家族
            case 4:
                initNewsListMap();
                iFamliyPresenter.getFamliyData(mMapNewsList);

        }

    }

    private void initNewsListMap() {
        mMapNewsList.put(URLConfig.URLFamliy.TAG_ID,URLConfig.URLVALUE.TAG_IDNEWSLIST);
        mMapNewsList.put(URLConfig.URLFamliy.PAGEINDEX,URLConfig.URLVALUE.PAGEINDEX);
        mMapNewsList.put(URLConfig.URLFamliy.SIGNATURE,URLConfig.URLVALUE.SIGNATURE_NEWSLIST);
        mMapNewsList.put(URLConfig.URLFamliy.PUBLICKEY,URLConfig.URLVALUE.PUBLICKEY);
        mMapNewsList.put(URLConfig.URLFamliy.TIMESTAMP,URLConfig.URLVALUE.TIMESTAMP_NEWSLIST);
        mMapNewsList.put(URLConfig.URLFamliy.TOKEN,URLConfig.URLVALUE.TOKEN);
        mMapNewsList.put(URLConfig.URLFamliy.NONCE,URLConfig.URLVALUE.NONCENEWSLIST);
    }

    private void initRiseListMap() {

        mMapRiseList.put(URLConfig.URLFamliy.TAG_ID,URLConfig.URLVALUE.TAG_IDRISELIST);
        mMapRiseList.put(URLConfig.URLFamliy.PAGEINDEX,URLConfig.URLVALUE.PAGEINDEX);
        mMapRiseList.put(URLConfig.URLFamliy.SIGNATURE,URLConfig.URLVALUE.SIGNATURE_RISELIST);
        mMapRiseList.put(URLConfig.URLFamliy.PUBLICKEY,URLConfig.URLVALUE.PUBLICKEY);
        mMapRiseList.put(URLConfig.URLFamliy.TIMESTAMP,URLConfig.URLVALUE.TIMESTAMP_RISELIST);
        mMapRiseList.put(URLConfig.URLFamliy.TOKEN,URLConfig.URLVALUE.TOKEN);
        mMapRiseList.put(URLConfig.URLFamliy.NONCE,URLConfig.URLVALUE.NONCERISELIST);

    }

    private void initHourList() {


        mMapHourList.put(URLConfig.URLFamliy.TAG_ID,URLConfig.URLVALUE.TAG_IDHOURLIST);
        mMapHourList.put(URLConfig.URLFamliy.PAGEINDEX,URLConfig.URLVALUE.PAGEINDEX);
        mMapHourList.put(URLConfig.URLFamliy.SIGNATURE,URLConfig.URLVALUE.SIGNATURE_HOURLIST);
        mMapHourList.put(URLConfig.URLFamliy.PUBLICKEY,URLConfig.URLVALUE.PUBLICKEY);
        mMapHourList.put(URLConfig.URLFamliy.TIMESTAMP,URLConfig.URLVALUE.TIMESTAMP_HOURLIST);
        mMapHourList.put(URLConfig.URLFamliy.TOKEN,URLConfig.URLVALUE.TOKEN);
        mMapHourList.put(URLConfig.URLFamliy.NONCE,URLConfig.URLVALUE.NONCEHOURLIST);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void showProgressBar() {
        progressBarFragment.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgressBar() {
        progressBarFragment.setVisibility(View.INVISIBLE);
    }

    @Override
    public void error(Throwable e) {
        Toast.makeText(getActivity(), "错误"+e, Toast.LENGTH_SHORT).show();
        StackTraceElement[] stackTrace = e.getStackTrace();
        for ( StackTraceElement  eeee:  stackTrace) {
           Log.e(TAG,"异常"+eeee.toString());
        }
    }


    @Override
    public void getFamliyData(FamliyBean famliyBean) {
    switch (mTabIndex){
        case 1:
            initRecyclerDaylist(famliyBean);
            break;
        case 2:
            initRecyclerDaylist(famliyBean);
            break;
        case 3:
            initRecyclerDaylist(famliyBean);
            break;
        case 4:
            initRecyclerDaylist(famliyBean);
            break;
    }




    }

    private void initRecyclerDaylist(FamliyBean famliyBean) {




        recycler_famliy_daylist.setHasFixedSize(true);
        recycler_famliy_daylist.setItemAnimator(new DefaultItemAnimator());
        recycler_famliy_daylist.addItemDecoration(new DividerItemDecoration(getActivity(),
                DividerItemDecoration.VERTICAL_LIST));
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity(),
                LinearLayoutManager.VERTICAL,false);
        recycler_famliy_daylist.setLayoutManager(linearLayoutManager);
        mListDaylist.clear();
        mListDaylist=famliyBean.getList();
        mDaylistAdapter=new FamliyRecyclerDaylistAdapter(getActivity(),mListDaylist);
        recycler_famliy_daylist.setAdapter(mDaylistAdapter);

        Log.i("============",mTabIndex+"   "+famliyBean);
        Log.i("============",mTabIndex+">>>>   "+famliyBean.getList().size());

        Log.e(TAG,famliyBean.getList().size()+"");
        Log.e(TAG,famliyBean.getList().get(2).getLivelist().size()+"");

//        mListDaylist.addAll(famliyBean.getList())
    }
}
